﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FrbaBus.Core;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace FrbaBus.Compra_de_Pasajes
{
    public partial class SeleccionarButacas : Form
    {
        public SeleccionarButacas()
        {
            InitializeComponent();
        }
        private CompraPasajes parent = null;
        public DataGridViewRow Row;
        public SeleccionarButacas (Form callingForm)
        {
            parent = callingForm as CompraPasajes;

            InitializeComponent();
        }

        private void SeleccionarButacas_Load(object sender, EventArgs e)
        {
            int i = 0;

            DBManager db = new DBManager();

            string fechaSalida = this.parent.diaHora.ToString();
            string origen = String.Copy(this.parent.textBox3.Text.ToString());
            string destino = String.Copy(this.parent.textBox4.Text.ToString());
            string patente = String.Copy(this.parent.Row.Cells[4].Value.ToString());
            string idRecorrido = String.Copy(this.parent.Row.Cells[3].Value.ToString());

            //int idRecorrido = id.

            //MessageBox.Show(fechaSalida);
            //MessageBox.Show(origen);
            //MessageBox.Show(destino);
            //MessageBox.Show(patente);
            //MessageBox.Show(idRecorrido);

            
             
            SqlCommand consultaX = new SqlCommand("exec ALL_IN.ButacasDisponibles @Fecha_Salida= '" + fechaSalida + "',@Origen= '" + origen + 
                "', @Destino= '" + destino + "', @Patente= '" + patente + "', @ID_recorrido= '" + idRecorrido + "'");
            SQLResponse r = db.executeQuery(consultaX);
            /*
            SqlCommand consulta2 = new SqlCommand("select @@rowcount");
            SQLResponse r2 = db.executeQuery(consulta2);
            int valor = (int)r2.result.Rows[0][0];
            */
            SqlCommand consulta12 = new SqlCommand("select @@rowcount");
            SQLResponse r12 = db.executeQuery(consulta12);
            int valor2 = (int)r12.result.Rows[0][0];

            //MessageBox.Show(valor2.ToString());
          
                for (i = 0; i < valor2; i++)
                {
                    dataGridView1.Rows.Add(r.result.Rows[i][0].ToString(), 
                        r.result.Rows[i][1].ToString(), r.result.Rows[i][2].ToString(), 
                        r.result.Rows[i][3].ToString());
                    dataGridView1.Rows[i].Cells[5].Value = "Selecione DNI";

                }

                foreach (DataGridViewColumn dc in dataGridView1.Columns)
                {
                    if (dc.Index.Equals(4))
                    {
                        dc.ReadOnly = false;
                    }
                    if (dc.Index.Equals(3))
                    {
                        dc.ReadOnly = true;
                    }
                    if (dc.Index.Equals(2))
                    {
                        dc.ReadOnly = true;
                    }


                }
                i = 0;
                if (parent.dataGridView1.Rows.Count > 0)
                {
                    for (int f = 0; f <= parent.dataGridView1.Rows.Count -1; f++)
                    {
                        for (i = 0; i <= dataGridView1.Rows.Count -1; i++)
                        {
                            if (parent.dataGridView1.Rows[f].Cells[1].Value.ToString() == dataGridView1.Rows[i].Cells[1].Value.ToString())
                            {

                                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[4];
                                chk.Value = chk.TrueValue;
                                //chk.Value = true;
                                dataGridView1.Rows[i].Cells[5].Value = string.Copy(parent.dataGridView1.Rows[f].Cells[0].Value.ToString());
                                dataGridView1.Rows[i].Cells[6].Value = string.Copy(parent.dataGridView1.Rows[f].Cells[6].Value.ToString());
                                dataGridView1.Rows[i].Cells[7].Value = string.Copy(parent.dataGridView1.Rows[f].Cells[5].Value.ToString());
                            }
                        }

                    }

                }


                parent.dataGridView1.Rows.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            int i = 0;
            
            i = 0;
            int b;
           // if (parent.dataGridView1.Rows.Count <= 0)
                b = 0;
          //  else
           // {
            //    b = parent.dataGridView1.Rows.Count -1;
            //}

            int discapacitado = 0;
            for (i = 0; i <= dataGridView1.Rows.Count -1; i++)
            {
                DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[i].Cells[4];
                if (chk.Value != null && chk.Value == chk.TrueValue)
                {
                   
                    if (string.Compare(dataGridView1.Rows[i].Cells[5].Value.ToString(), "Seleccione DNI") < 0 )
                    {
                        
                        parent.dataGridView1.Rows.Add(1);
                        parent.dataGridView1.Rows[b].Cells[0].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[5].Value.ToString());
                        parent.dataGridView1.Rows[b].Cells[1].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[1].Value.ToString());
                        parent.dataGridView1.Rows[b].Cells[2].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[2].Value.ToString());
                        parent.dataGridView1.Rows[b].Cells[3].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[3].Value.ToString());
                        parent.dataGridView1.Rows[b].Cells[5].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[7].Value.ToString());
                        parent.dataGridView1.Rows[b].Cells[6].Value =
                            string.Copy(dataGridView1.Rows[i].Cells[6].Value.ToString());
                        DBManager db = new DBManager();
                        SqlCommand consulta = new SqlCommand("select Porcentaje_Adicional from ALL_IN.Tipo_Servicio where Tipo_Servicio = '" + parent.dataGridView2.SelectedCells[2].Value.ToString() + "'");
                        SQLResponse r = db.executeQuery(consulta);
                        decimal adicio = Convert.ToDecimal(r.result.Rows[0][0].ToString());

                        // precio
                        parent.dataGridView1.Rows[b].Cells[4].Value = (Convert.ToDecimal(parent.dataGridView2.SelectedCells[7].Value.ToString()) * adicio).ToString();

                        if (dataGridView1.Rows[i].Cells[7].Value.ToString() == "1")// jubilado
                        {
                            decimal valor = Convert.ToDecimal(parent.dataGridView1.Rows[b].Cells[4].Value.ToString());
                            parent.dataGridView1.Rows[b].Cells[4].Value = (valor / 2).ToString();
                        }
                        if (dataGridView1.Rows[i].Cells[6].Value.ToString() == "1")// discapacidad
                        {
                            discapacitado = 1;
                            parent.dataGridView1.Rows[b].Cells[4].Value = "0";
                        }

                       
                        b++;
                    }
                }

            }
            int control = 0;
            if (discapacitado == 1 )
            {
                for (i = 0; (i <= parent.dataGridView1.Rows.Count - 1) && control == 0; i++)
                {
                    if (parent.dataGridView1.Rows[i].Cells[6].Value.ToString() == "0")
                    {
                        parent.dataGridView1.Rows[i].Cells[4].Value = "0";
                        control = 1;
                    }
                }
            }
            this.Close();
            
            
        }

        /*
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 
        {}
         */

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not in our 
            if (e.ColumnIndex == dataGridView1.Columns[5].Index && e.RowIndex >= 0)
            {
                //Console.WriteLine("Button on row {0} clicked", e.RowIndex);
                int fila = Convert.ToInt16(e.RowIndex);
                //MessageBox.Show("FILA: "+fila.ToString());
                //Row = dataGridView1.SelectedRows[fila];
                
                //MessageBox.Show("INDICE: "+dataGridView1.Columns[4].Index.ToString());
                //MessageBox.Show("CELL: "+dataGridView1.SelectedRows.ToString());

                if (dataGridView1.SelectedRows.Count != 0)
                {

                    Row = dataGridView1.SelectedRows[0];
                    Compra_de_Pasajes.Cliente frm = new Compra_de_Pasajes.Cliente(this);
                    frm.ShowDialog(this);

//                    this.Close();
                }


            }

        }


 


        
    }


}
